package com.gitee.magic.framework.base.rest;

import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.core.utils.reflect.TypeReference;
import com.gitee.magic.framework.head.vo.BaseVO;

/**
 * @author start
 */
public class ResponseResolve {

	private String content;
	private JsonObject responseJson;

	public ResponseResolve(String content) {
		try {
			this.content=content;
			this.responseJson = new JsonObject(content);
		} catch (Exception e) {
			throw new JsonResultException(e,content);
		}
	}

	public void checkResponse(BaseVO response) {
		int code=response.getCode();
		int startCode=200;
		int endCode=300;
		if(!(code>=startCode&&code<endCode)) {
			throw new BusinessRestException(this.content, response.getCode(), response.getMessage(), response.getLogno(),response.getName());
		}
	}
	
	public <T>T analysis(Class<T> prototype){
		return ConverterEditorUtils.restoreObject(prototype, this.responseJson);
	}
	
	public <T,R>R analysis(Class<T> prototype,TypeReference<R> references){
		return ConverterEditorUtils.restoreObject(prototype, this.responseJson,references);
	}
	
}
